package emulator.shell.GUI.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import emulator.assembler.Assembler;
import emulator.hardware.memory.MemoryBlockInterface;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.shell.GUI.debugger.DebugPanel;
import emulator.shell.GUI.windows.ManagedWindow;
import emulator.shell.GUI.windows.WindowManager;

public class WindowDebug implements ActionListener {

	private int window_id;
	private WindowManager window_manager;
	private MemoryBlockInterface memory;
	private Assembler assembler;
	private Cpu6502 cpu;
	private DebugPanel debug_panel;
	
	public WindowDebug(WindowManager window_manager, int window_id, Assembler assembler, Cpu6502 cpu, MemoryBlockInterface memory)
	{
		this.window_manager = window_manager;
		this.window_id = window_id;
		this.assembler = assembler;
		this.cpu = cpu;
		this.memory = memory;
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if (window_manager.isWindowActive(window_id))
			closeWindow();
		else
			openWindow();
	}

	private void openWindow() {
		ManagedWindow window = window_manager.createWindow(window_id, "Debug");
		debug_panel = new DebugPanel(window, assembler, cpu, memory.getData());
		window.getContentPane().add(debug_panel);
		window.restorePosition();
		window.setVisible(true);
	}

	private void closeWindow() {
		debug_panel.shutdown();
		window_manager.closeWindow(window_id);
	}
}
